package health.lm.com.mine.activity;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.arouter.Router;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.adapter.IntervalRankingListAdapter;
import health.lm.com.mine.entity.IntegralRankingListEntity;
import health.lm.com.mine.present.IntegralDetailListPresenter;
import health.lm.com.mine.present.IntegralRankingListPresenter;
import health.lm.com.router.ARouterConstant;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;
import static health.lm.com.component_base.arouter.Router.WebActivity;

/**
 * 积分排行榜
 *
 * @author GQI
 */
@Route(path = ARouterConstant.IntegralRankingListActivity)
public class IntegralRankingListActivity extends XListActivity<IntegralRankingListPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R2.id.rlv_jf_ranking_list)
    RecyclerView rlvJfRankingList;
    private List<IntegralRankingListEntity.Rank_list> beanList;
    private IntervalRankingListAdapter adapter;
    private IntegralRankingListEntity bean;
    private boolean isRefresh;
    private TextView tvCount,tvRanking;

    @Override
    public int getLayoutId() {
        return R.layout.activity_jf_ranking_list;
    }

    @Override
    public IntegralRankingListPresenter newP() {
        return new IntegralRankingListPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initAdapter();
        super.rlRefreshLayout = srlLayout;
        super.adapter = adapter;
        super.recyclerView = rlvJfRankingList;
        super.initData(savedInstanceState);
        titlebar.setListener((v, action, extra) -> {
            if (action == ACTION_LEFT_BUTTON) {
                finish();
            }
        });
    }

    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        this.isRefresh = isRefresh;
        getP().getListMore(isRefresh, layout, page, pageSize);
    }


    /**
     * 初始化适配器
     */
    private void initAdapter() {
        beanList = new ArrayList<>();
        adapter = new IntervalRankingListAdapter(beanList);
        adapter.addHeaderView(getHeaderView());
        rlvJfRankingList.setLayoutManager(new LinearLayoutManager(mActivity));
        rlvJfRankingList.setAdapter(adapter);
    }
    /**
     * 初始化recycleview 头部view
     */
    private View getHeaderView() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_jf_ranking_list_top, null);
        tvCount = view.findViewById(R.id.tv_count);
        tvRanking = view.findViewById(R.id.tv_ranking);
        view.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return view;
    }
    public void getListSuccess(IntegralRankingListEntity bean) {
        this.beanList = bean.getRank_list();
        this.bean = bean;
//        if (isRefresh && beanList.size() >= pageSize) {
//            initLoadMore();
//        }
        tvCount.setText(bean.getMy_credit());
        tvRanking.setText(bean.getMy_order());
        notifyData();
    }


    private void notifyData() {
        if (beanList == null) {
            return;
        }
//        if (beanList.size() < pageSize) {
//            adapter.loadMoreEnd(false);
//        } else {
//            adapter.loadMoreComplete();
//        }
        if (isRefresh) {
            adapter.setNewData(beanList);
//        } else {
//            adapter.addData(beanList);
        }
    }

}
